﻿namespace BlackHole.DAO
{
    using System;
    using System.Collections.Generic;
    using System.Configuration;
    using System.Data;
    using System.Data.SqlClient;

    public static partial class Utility
    {
        public static bool ProductCheckIn(int prodcutId)
        {
            int ret;
            using (SqlConnection conn = new SqlConnection(ConnStr))
            {
                SqlCommand cmd = new SqlCommand("ProductCheckIn", conn);
                cmd.CommandType = CommandType.StoredProcedure;
                cmd.Parameters.AddWithValue("@productId", prodcutId);
                cmd.Parameters.Add("@Return", SqlDbType.Int).Direction = ParameterDirection.ReturnValue;
                conn.Open();
                cmd.ExecuteNonQuery();
                ret = (int)cmd.Parameters["@Return"].Value;
                return
                    ret != 0 ? true : false;
            }
        }

        public static bool ProductRepair(int productId, int requestFaultId, int realFaultId, string repairMethod, string repairGear)
        {
            using (SqlConnection conn = new SqlConnection(ConnStr))
            {
                int ret;
                SqlCommand cmd = new SqlCommand("ProductRepair", conn);
                cmd.CommandType = CommandType.StoredProcedure;
                cmd.Parameters.AddWithValue("@productId", productId);
                cmd.Parameters.AddWithValue("@requestFaultId", requestFaultId);
                cmd.Parameters.AddWithValue("@realFaultId", realFaultId);
                cmd.Parameters.AddWithValue("@repairMethod", repairMethod);
                cmd.Parameters.AddWithValue("@repairGear", repairGear);
                cmd.Parameters.Add("@Return", SqlDbType.Int).Direction = ParameterDirection.ReturnValue;
                conn.Open();
                cmd.ExecuteNonQuery();
                ret = (int)cmd.Parameters["@Return"].Value;
                return
                    ret != 0 ? true : false;
            }
        }

        public static bool ProductCheckOut(int prodcutId)
        {
            int ret;
            using (SqlConnection conn = new SqlConnection(ConnStr))
            {
                SqlCommand cmd = new SqlCommand("ProductCheckOut", conn);
                cmd.CommandType = CommandType.StoredProcedure;
                cmd.Parameters.AddWithValue("@productId", prodcutId);
                cmd.Parameters.Add("@Return", SqlDbType.Int).Direction = ParameterDirection.ReturnValue;
                conn.Open();
                cmd.ExecuteNonQuery();
                ret = (int)cmd.Parameters["@Return"].Value;
                return
                    ret != 0 ? true : false;
            }
        }

        public static bool ProductUpdate(int productId, int newOwnerId, int newDeviceId, string serialNO, string psamNO, string romVersion)
        {
            int ret;
            using (SqlConnection conn = new SqlConnection(ConnStr))
            {
                SqlCommand cmd = new SqlCommand("ProductUpdate", conn);
                cmd.CommandType = CommandType.StoredProcedure;
                cmd.Parameters.AddWithValue("@productId", productId);
                cmd.Parameters.AddWithValue("@newOwnerId", newOwnerId);
                cmd.Parameters.AddWithValue("@newDeviceId", newDeviceId);
                cmd.Parameters.AddWithValue("@serialNO", serialNO);
                cmd.Parameters.AddWithValue("@psamNO", psamNO);
                cmd.Parameters.AddWithValue("@romVersion", romVersion);
                cmd.Parameters.Add("@Return", SqlDbType.Int).Direction = ParameterDirection.ReturnValue;
                conn.Open();
                cmd.ExecuteNonQuery();
                ret = (int)cmd.Parameters["@Return"].Value;
                return
                    ret == 0 ? false : true;
            }
        }

        public static bool ProductAddNew(int deviceId, int ownerId, string serialNO, string psamNO, string romVersion)
        {
            int ret;
            using (SqlConnection conn = new SqlConnection(ConnStr))
            {
                SqlCommand cmd = new SqlCommand("ProductAddNew", conn);
                cmd.CommandType = CommandType.StoredProcedure;
                cmd.Parameters.AddWithValue("@deviceId", deviceId);
                cmd.Parameters.AddWithValue("@ownerId", ownerId);
                cmd.Parameters.AddWithValue("@serialNO", serialNO);
                cmd.Parameters.AddWithValue("@psamNO", psamNO);
                cmd.Parameters.AddWithValue("@romVersion", romVersion);
                cmd.Parameters.Add("@Return", SqlDbType.Int).Direction = ParameterDirection.ReturnValue;
                conn.Open();
                try
                {
                    cmd.ExecuteNonQuery();
                    ret = (int)cmd.Parameters["@Return"].Value;
                    return
                        ret == 0 ? false : true;
                }
                catch (Exception)
                {
                    throw;
                }
            }
        }

        public static bool ProductAddAndRepair(int deviceId, int ownerId, string serialNO, string psamNO, string romVersion, int requestFaultId, int realFaultId, string repairMethod, string repairGear)
        {
            int ret;
            using (SqlConnection conn = new SqlConnection(ConnStr))
            {
                SqlCommand cmd = new SqlCommand("ProductAddNewAndRepair", conn);
                cmd.CommandType = CommandType.StoredProcedure;
                cmd.Parameters.AddWithValue("@deviceId", deviceId);
                cmd.Parameters.AddWithValue("@ownerId", ownerId);
                cmd.Parameters.AddWithValue("@serialNO", serialNO);
                cmd.Parameters.AddWithValue("@psamNO", psamNO);
                cmd.Parameters.AddWithValue("@romVersion", romVersion);
                cmd.Parameters.AddWithValue("@requestFaultId", requestFaultId);
                cmd.Parameters.AddWithValue("@realFaultId", realFaultId);
                cmd.Parameters.AddWithValue("@repairMethod", repairMethod);
                cmd.Parameters.AddWithValue("@repairGear", repairGear);
                cmd.Parameters.Add("@Return", SqlDbType.Int).Direction = ParameterDirection.ReturnValue;
                conn.Open();
                try
                {
                    cmd.ExecuteNonQuery();
                    ret = (int)cmd.Parameters["@Return"].Value;
                    return
                        ret == 0 ? false : true;
                }
                catch (Exception)
                {
                    throw;
                }
            }
        }
    }
}